package com.llkj.contractmanagement.customview.photoview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;


import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.utils.ImageLoaderUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 查看大图
 */
public class ActivityLookBig extends Activity implements PhotoViewAttacher.OnPhotoTapListener {
    private HackyViewPager viewPager;
    private List<View> pageViews;
    private ImageView imageView;
    private ImageView[] imageViews;
    // 包裹小圆点的LinearLayout
    private ViewGroup group;
    private Intent intent;
    private ArrayList<String> image_list;
    private GuidePageAdapter adapter;
    private int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_big_image);
        initView();
    }

    @SuppressWarnings({"deprecation", "unchecked"})
    private void initView() {
        intent = getIntent();
        image_list = (ArrayList<String>) intent
                .getSerializableExtra("image_list");
        i = intent.getIntExtra("position", 0);
        group = (ViewGroup) findViewById(R.id.viewGroup);
        viewPager = (HackyViewPager) findViewById(R.id.guidePages);
        pageViews = new ArrayList<View>();
        for (int i = 0; i < image_list.size(); i++) {
            LinearLayout layout = new LinearLayout(this);
            LayoutParams ltp = new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT);
            final PhotoView imageView = new PhotoView(this);
            imageView.setScaleType(ScaleType.FIT_CENTER);
            imageView.setOnPhotoTapListener(this);
            ImageLoaderUtil.displayImage(image_list.get(i), imageView);
            layout.addView(imageView, ltp);
            pageViews.add(layout);
        }
        imageViews = new ImageView[pageViews.size()];
        for (int i = 0; i < pageViews.size(); i++) {
            imageView = new ImageView(ActivityLookBig.this);
            imageView.setLayoutParams(new LayoutParams(10, 10));
            imageView.setPadding(5, 0, 5, 0);
            imageViews[i] = imageView;
            if (i == 0) {
                // 默认选中第一张图片
                imageViews[i]
                        .setBackgroundResource(R.drawable.level_page_indicator_selected);
            } else {
                imageViews[i]
                        .setBackgroundResource(R.drawable.level_page_indicator);
            }
            group.addView(imageViews[i]);
        }
        adapter = new GuidePageAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new GuidePageChangeListener());
        viewPager.setCurrentItem(i);
    }

    // 指引页面数据适配器
    class GuidePageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pageViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(pageViews.get(arg1));
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(pageViews.get(arg1));
            return pageViews.get(arg1);
        }
    }

    // 指引页面更改事件监听器
    class GuidePageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0; i < imageViews.length; i++) {
                imageViews[arg0]
                        .setBackgroundResource(R.drawable.level_page_indicator_selected);

                if (arg0 != i) {
                    imageViews[i]
                            .setBackgroundResource(R.drawable.level_page_indicator);
                }
            }
        }
    }

    @Override
    public void onPhotoTap(View view, float x, float y) {
        this.finish();
    }
}
